package service

import (
	"anban/models"
	"anban/utils"
	"fmt"
	"github.com/astaxie/beego/orm"
	"net/url"
)

// 获取学生会议通知信息
func GetMeetingNoticeInfo(p map[string]interface{}) *models.MeetingNotice {
	o := orm.NewOrm()
	meetingNotice := &models.MeetingNotice{}
	qs := o.QueryTable("MeetingNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	qs.One(meetingNotice)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if meetingNotice.Id > 0 {
			meetingNotice.CreatedShowDate = meetingNotice.Created.Format("2006.1.2")
			meetingNotice.CreatedShow = meetingNotice.Created.Format("2006-01-02 15:04:05")
		}
	}
	return meetingNotice
}

// 添加单条学生会议通知
func AddMeetingNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	meetingNotice := &models.MeetingNotice{}
	student := &models.Student{}
	if _, ok := input["student_id"]; ok {
		student.Id = utils.Atoi64(input["student_id"][0])
		o.Read(student)
	}
	meetingNotice.Student = student
	meeting := &models.Meeting{}
	if _, ok := input["meeting_id"]; ok {
		meeting.Id = utils.Atoi64(input["meeting_id"][0])
		o.Read(meeting)
	}
	meetingNotice.Meeting = meeting
	return o.Insert(meetingNotice)
}

// 添加多条学生会议通知
func AddMeetingNotices(input url.Values) (int64, error) {
	o := orm.NewOrm()
	var meetingNotices []models.MeetingNotice
	meeting := &models.Meeting{}
	if _, ok := input["meeting_id"]; ok {
		meeting.Id = utils.Atoi64(input["meeting_id"][0])
		o.Read(meeting)
	}
	p := map[string]interface{}{}
	p["classId"] = input["class_id"][0]
	_, studentList := GetStudentList(p)
	for _, student := range studentList {
		if student.Status == 1 {
			meetingNotice := models.MeetingNotice{}
			meetingNotice.Meeting = meeting
			meetingNotice.Student = student
			meetingNotices = append(meetingNotices, meetingNotice)
		}
	}
	return o.InsertMulti(1, meetingNotices)
}

// 修改学生会议通知信息
func EditMeetingNotice(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("MeetingNotice").Filter("id", input["id"][0]).Update(p)
}

// 获取学生会议通知列表
func GetMeetingNoticeList(p map[string]interface{}) (int64, []*models.MeetingNotice) {
	var meetingNoticeList []*models.MeetingNotice
	o := orm.NewOrm()
	qs := o.QueryTable("MeetingNotice")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["studentIds"]; ok && len(p["studentIds"].([]int64)) > 0 {
		qs = qs.Filter("student_id__in", p["studentIds"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if _, isLimit := p["limit"]; isLimit {
		qs = qs.Limit(p["limit"])
	} else if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&meetingNoticeList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, meetingNotice := range meetingNoticeList {
			meetingNotice.CreatedShowDate = meetingNotice.Created.Format("2006.1.2")
			meetingNotice.CreatedShow = meetingNotice.Created.Format("2006-01-02 15:04:05")
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		for _, meetingNotice := range meetingNoticeList {
			meetingNotice.StudentName = meetingNotice.Student.RealName
			meetingNotice.TeacherName = meetingNotice.Meeting.User.RealName
			meetingNotice.TeacherPhone = meetingNotice.Meeting.User.Phone
			meetingNotice.MeetingName = fmt.Sprintf("%s家长会", meetingNotice.Student.Class.Name)
			meetingNotice.MeetingContent = meetingNotice.Meeting.Content
			meetingNotice.CreatedShowDate = meetingNotice.Created.Format("2006.1.2")
		}
	}
	return totalCount, meetingNoticeList
}
